package com.teach.controller;



import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.teach.R;
import com.teach.commons.FileUtil;
import com.teach.service.FileService;

@Controller
@RequestMapping("/ueditor/jsp/")
public class UeditorController {
	

	
	@Autowired
	private FileService fileService;
	
	@RequestMapping("controller.do")
	@ResponseBody
	public R doUpload(MultipartFile upfile,String action,HttpServletResponse response)throws Exception {
		switch (action) {
		case "config":
			response.sendRedirect("config.json");
			return null;

		default:
			R json = R.empty();
			json.put("state", "SUCCESS");
			json.put("original", upfile.getOriginalFilename());
			json.put("title", upfile.getOriginalFilename());
			json.put("size", upfile.getSize());
			json.put("type", FileUtil.getFileExt(upfile.getOriginalFilename()));
			json.put("url", fileService.saveImage(upfile));
			
			return json;
		}
		
	}

}
